<template>
  <div class="report-management">
    <!-- 查询表单 -->
    <el-form class="demo-form-inline" ref="formRef" :model="queryForm" label-width="100px" size="small" inline>
      <el-form-item label="文件名" prop="fileName">
        <el-input v-model="queryForm.fileName" placeholder="请输入文件名" clearable></el-input>
      </el-form-item>
      <el-form-item label="扫描器" prop="scanner">
        <el-select v-model="queryForm.scanner" placeholder="请选择扫描器" clearable>
          <el-option label="扫描器1" value="扫描器1"></el-option>
          <el-option label="扫描器2" value="扫描器2"></el-option>
          <el-option label="扫描器3" value="扫描器3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作栏 -->
    <div class="operation-bar">
      <el-button type="success" icon="el-icon-document-add" plain size="small" :disabled="selectedRows.length !== 2" @click="showMergeReportDialog">整合报告</el-button>
      <el-button type="danger" icon="el-icon-delete" plain size="small" :disabled="selectedRows.length === 0" @click="deleteSelectedReports">批量删除</el-button>
    </div>

    <!-- 表格列表 -->
    <div class="table-container">
      <el-table
        ref="multipleTable"
        :data="filteredTableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="loading"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column prop="fileName" label="文件名"></el-table-column>
        <el-table-column prop="type" label="类型"></el-table-column>
        <el-table-column prop="scanner" label="扫描器"></el-table-column>
        <el-table-column prop="fileContent" label="文件内容"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="previewFile(scope.row)">预览</el-button>
            <el-button type="text" @click="editFile(scope.row)">编辑</el-button>
            <el-button type="text" @click="deleteFile(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="text-align: right; margin-top: 20px;"
    ></el-pagination>

    <!-- 预览对话框 -->
    <PreviewDialog ref="previewDialog" />

    <!-- 编辑对话框 -->
    <EditDialog ref="editDialog" @save="handleSave" />

    <!-- 整合报告对话框 -->
    <MergeReportDialog ref="mergeReportDialog" @merged="handleMerged" />
  </div>
</template>

<script>
import { Message, MessageBox } from 'element-ui';
import PreviewDialog from './components/PreviewDialog.vue';
import EditDialog from './components/EditDialog.vue';
import MergeReportDialog from './components/MergeReportDialog.vue';

export default {
  components: {
    PreviewDialog,
    EditDialog,
    MergeReportDialog
  },
  data() {
    return {
      queryForm: {
        fileName: '',
        scanner: ''
      },
      tableData: [
        // 示例数据
        { index: 1, fileName: 'Report1.docx', type: 'Word', scanner: '扫描器1', fileContent: 'This is the content of Report 1.' },
        { index: 2, fileName: 'Report2.docx', type: 'Word', scanner: '扫描器2', fileContent: 'This is the content of Report 2.' },
        { index: 3, fileName: 'Report3.docx', type: 'Word', scanner: '扫描器3', fileContent: 'This is the content of Report 3.' },
      ],
      selectedRows: [],
      total: 10,
      pageSize: 10,
      currentPage: 1,
      loading: false
    };
  },
  computed: {
    filteredTableData() {
      const { fileName, scanner } = this.queryForm;
      let filteredData = this.tableData;

      if (fileName) {
        filteredData = filteredData.filter(item => item.fileName.includes(fileName));
      }

      if (scanner) {
        filteredData = filteredData.filter(item => item.scanner === scanner);
      }

      this.total = filteredData.length;
      return filteredData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
    }
  },
  methods: {
    handleSearch() {
      this.fetchDocuments();
    },
    resetForm() {
      this.$refs.formRef.resetFields();
      this.fetchDocuments();
    },
    fetchDocuments() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 1000);
    },
    handleSelectionChange(val) {
      this.selectedRows = val;
    },
    showMergeReportDialog() {
      if (this.selectedRows.length === 2) {
        this.$refs.mergeReportDialog.open(this.selectedRows);
      } else {
        Message.warning('请选择两个列表项进行整合');
      }
    },
    deleteSelectedReports() {
      MessageBox.confirm('此操作将永久删除选中的文件信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const idsToDelete = this.selectedRows.map(row => row.index);
        this.tableData = this.tableData.filter(row => !idsToDelete.includes(row.index));
        this.selectedRows = [];
        Message.success('删除成功');
      }).catch(() => {
        Message.info('已取消删除');
      });
    },
    previewFile(row) {
      this.$refs.previewDialog.open(row.fileContent);
    },
    editFile(row) {
      this.$refs.editDialog.open(row);
    },
    deleteFile(row) {
      MessageBox.confirm('此操作将永久删除该文件信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableData = this.tableData.filter(item => item.index !== row.index);
        Message.success('删除成功');
      }).catch(() => {
        Message.info('已取消删除');
      });
    },
    handleSave(updatedRow) {
      const targetIndex = this.tableData.findIndex(row => row.id === updatedRow.id);
      if (targetIndex !== -1) {
        this.$set(this.tableData, targetIndex, {
          ...this.tableData[targetIndex],
          fileContent: updatedRow.fileContent
        });
        Message.success('保存成功');
      }
    },
    handleMerged(mergedReport) {
      mergedReport.id = this.nextId++;
      this.tableData.push(mergedReport);
      Message.success('整合报告保存成功');
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchDocuments();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchDocuments();
    },
  }
};
</script>

<style scoped>
.report-management {
  padding: 20px;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.operation-bar {
  margin-bottom: 20px;
}

.table-container {
  flex: 1;
}
</style>
